package com.dianping.cat.home.router.transform;

import com.dianping.cat.home.router.IEntity;
import com.dianping.cat.home.router.IVisitor;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
























public class DefaultJsonBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  51 */   public DefaultJsonBuilder() { this(false); }



/*  55 */   public DefaultJsonBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }


  public DefaultJsonBuilder(boolean compact, StringBuilder sb) {
/*  59 */     this.m_compact = compact;
/*  60 */     this.m_sb = sb;
/*  61 */     this.m_visitor = this;
  }

  protected void arrayBegin(String name) {
/*  65 */     indent();
/*  66 */     this.m_sb.append('"').append(name).append(this.m_compact ? "\":[" : "\": [\r\n");
/*  67 */     this.m_level++;
  }

  protected void arrayEnd(String name) {
/*  71 */     this.m_level--;

/*  73 */     trimComma();
/*  74 */     indent();
/*  75 */     this.m_sb.append("],").append(this.m_compact ? "" : "\r\n");
  }

  protected void attributes(Map<String, String> dynamicAttributes, Object... nameValues) {
/*  79 */     int len = nameValues.length;

/*  81 */     for (int i = 0; i + 1 < len; i += 2) {
/*  82 */       Object attrName = nameValues[i];
/*  83 */       Object attrValue = nameValues[i + 1];

/*  85 */       if (attrValue != null) {
/*  86 */         if (attrValue instanceof Collection) {

/*  88 */           Collection<Object> items = (Collection)attrValue;

/*  90 */           if (!items.isEmpty()) {
/*  91 */             indent();
/*  92 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/*  94 */             for (Object item : items) {
              this.m_sb.append(' ');
/*  96 */               toString(this.m_sb, item);
/*  97 */               this.m_sb.append(',');
            }

/* 100 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 101 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }
/* 103 */         } else if (attrValue.getClass().isArray()) {
/* 104 */           int length = Array.getLength(attrValue);

/* 106 */           if (length > 0) {
/* 107 */             indent();
/* 108 */             this.m_sb.append('"').append(attrName).append(this.m_compact ? "\":[" : "\": [");

/* 110 */             for (int j = 0; j < length; j++) {
/* 111 */               Object item = Array.get(attrValue, j);
/* 112 */               this.m_sb.append(' ');
/* 113 */               toString(this.m_sb, item);
/* 114 */               this.m_sb.append(',');
            }

/* 117 */             this.m_sb.setLength(this.m_sb.length() - 1);
/* 118 */             this.m_sb.append(this.m_compact ? "]," : " ],\r\n");
          }

/* 121 */         } else if (this.m_compact) {
/* 122 */           this.m_sb.append('"').append(attrName).append("\":");
/* 123 */           toString(this.m_sb, attrValue);
/* 124 */           this.m_sb.append(",");
        } else {
/* 126 */           indent();
/* 127 */           this.m_sb.append('"').append(attrName).append("\": ");
/* 128 */           toString(this.m_sb, attrValue);
/* 129 */           this.m_sb.append(",\r\n");
        }
      }
    }


/* 135 */     if (dynamicAttributes != null) {
/* 136 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 137 */         if (this.m_compact) {
/* 138 */           this.m_sb.append('"').append((String)e.getKey()).append("\":");
/* 139 */           toString(this.m_sb, e.getValue());
/* 140 */           this.m_sb.append(","); continue;
        }
/* 142 */         indent();
/* 143 */         this.m_sb.append('"').append((String)e.getKey()).append("\": ");
/* 144 */         toString(this.m_sb, e.getValue());
/* 145 */         this.m_sb.append(",\r\n");
      }
    }
  }


  public String build(IEntity<?> entity) {
/* 152 */     objectBegin(null);
/* 153 */     entity.accept(this);
/* 154 */     objectEnd(null);
/* 155 */     trimComma();

/* 157 */     return this.m_sb.toString();
  }

  public String buildArray(Collection<? extends IEntity<?>> entities) {
/* 161 */     this.m_sb.append('[');

/* 163 */     if (entities != null) {
/* 164 */       for (IEntity<?> entity : entities) {
/* 165 */         objectBegin(null);
/* 166 */         entity.accept(this);
/* 167 */         objectEnd(null);
      }

/* 170 */       trimComma();
    }

/* 173 */     this.m_sb.append(']');

/* 175 */     return this.m_sb.toString();
  }

  protected void indent() {
/* 179 */     if (!this.m_compact) {
/* 180 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 181 */         this.m_sb.append("   ");
      }
    }
  }

  protected void objectBegin(String name) {
/* 187 */     indent();

/* 189 */     if (name == null) {
/* 190 */       this.m_sb.append("{").append(this.m_compact ? "" : "\r\n");
    } else {
/* 192 */       this.m_sb.append('"').append(name).append(this.m_compact ? "\":{" : "\": {\r\n");
    }

/* 195 */     this.m_level++;
  }

  protected void objectEnd(String name) {
/* 199 */     this.m_level--;

/* 201 */     trimComma();
/* 202 */     indent();
/* 203 */     this.m_sb.append(this.m_compact ? "}," : "},\r\n");
  }

  protected String toString(Date date, String format) {
/* 207 */     if (date != null) {
/* 208 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 210 */     return null;
  }


  protected void toString(StringBuilder sb, Object value) {
/* 215 */     if (value == null) {
/* 216 */       sb.append("null");
/* 217 */     } else if (value instanceof Boolean || value instanceof Number) {
/* 218 */       sb.append(value);
    } else {
/* 220 */       String val = value.toString();
/* 221 */       int len = val.length();

/* 223 */       sb.append('"');

/* 225 */       for (int i = 0; i < len; i++) {
/* 226 */         char ch = val.charAt(i);

/* 228 */         switch (ch) {
          case '"':
          case '/':
          case '\\':
/* 232 */             sb.append('\\').append(ch);
            break;
          case '\t':
/* 235 */             sb.append("\\t");
            break;
          case '\r':
/* 238 */             sb.append("\\r");
            break;
          case '\n':
/* 241 */             sb.append("\\n");
            break;
          default:
/* 244 */             sb.append(ch);
            break;
        }

      }
/* 249 */       sb.append('"');
    }
  }

  protected void trimComma() {
/* 254 */     int len = this.m_sb.length();

/* 256 */     if (this.m_compact) {
/* 257 */       if (len > 1 && this.m_sb.charAt(len - 1) == ',') {
/* 258 */         this.m_sb.replace(len - 1, len, "");
      }
    }
/* 261 */     else if (len > 3 && this.m_sb.charAt(len - 3) == ',') {
/* 262 */       this.m_sb.replace(len - 3, len - 2, "");
    }
  }




/* 269 */   public void visitDefaultServer(DefaultServer defaultServer) { attributes(null, new Object[] { "id", defaultServer.getId(), "weight", Double.valueOf(defaultServer.getWeight()), "port", Integer.valueOf(defaultServer.getPort()), "enable", Boolean.valueOf(defaultServer.isEnable()) }); }



  public void visitDomain(Domain domain) {
/* 274 */     attributes(null, new Object[] { "id", domain.getId() });

/* 276 */     if (!domain.getGroups().isEmpty()) {
/* 277 */       objectBegin("groups");

/* 279 */       for (Map.Entry<String, Group> e : domain.getGroups().entrySet()) {
/* 280 */         String key = String.valueOf(e.getKey());

/* 282 */         objectBegin(key);
/* 283 */         ((Group)e.getValue()).accept(this.m_visitor);
/* 284 */         objectEnd(key);
      }

/* 287 */       objectEnd("groups");
    }
  }


  public void visitGroup(Group group) {
/* 293 */     attributes(null, new Object[] { "id", group.getId() });

/* 295 */     if (!group.getServers().isEmpty()) {
/* 296 */       arrayBegin("servers");

/* 298 */       for (Server server : group.getServers()) {
/* 299 */         objectBegin(null);
/* 300 */         server.accept(this.m_visitor);
/* 301 */         objectEnd(null);
      }

/* 304 */       arrayEnd("servers");
    }
  }



/* 310 */   public void visitGroupServer(GroupServer groupServer) { attributes(null, new Object[] { "id", groupServer.getId() }); }




/* 315 */   public void visitNetwork(Network network) { attributes(null, new Object[] { "id", network.getId() }); }



  public void visitNetworkPolicy(NetworkPolicy networkPolicy) {
/* 320 */     attributes(null, new Object[] { "id", networkPolicy.getId(), "title", networkPolicy.getTitle(), "block", Boolean.valueOf(networkPolicy.isBlock()), "server-group", networkPolicy.getServerGroup() });

/* 322 */     if (!networkPolicy.getNetworks().isEmpty()) {
/* 323 */       objectBegin("networks");

/* 325 */       for (Map.Entry<String, Network> e : networkPolicy.getNetworks().entrySet()) {
/* 326 */         String key = String.valueOf(e.getKey());

/* 328 */         objectBegin(key);
/* 329 */         ((Network)e.getValue()).accept(this.m_visitor);
/* 330 */         objectEnd(key);
      }

/* 333 */       objectEnd("networks");
    }
  }


  public void visitRouterConfig(RouterConfig routerConfig) {
/* 339 */     attributes(null, new Object[] { "backup-server", routerConfig.getBackupServer(), "backup-server-port", Integer.valueOf(routerConfig.getBackupServerPort()), "startTime", toString(routerConfig.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "domain", routerConfig.getDomain(), "endTime", toString(routerConfig.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 341 */     if (!routerConfig.getDefaultServers().isEmpty()) {
/* 342 */       objectBegin("default-servers");

/* 344 */       for (Map.Entry<String, DefaultServer> e : routerConfig.getDefaultServers().entrySet()) {
/* 345 */         String key = String.valueOf(e.getKey());

/* 347 */         objectBegin(key);
/* 348 */         ((DefaultServer)e.getValue()).accept(this.m_visitor);
/* 349 */         objectEnd(key);
      }

/* 352 */       objectEnd("default-servers");
    }

/* 355 */     if (!routerConfig.getNetworkPolicies().isEmpty()) {
/* 356 */       objectBegin("network-policies");

/* 358 */       for (Map.Entry<String, NetworkPolicy> e : routerConfig.getNetworkPolicies().entrySet()) {
/* 359 */         String key = String.valueOf(e.getKey());

/* 361 */         objectBegin(key);
/* 362 */         ((NetworkPolicy)e.getValue()).accept(this.m_visitor);
/* 363 */         objectEnd(key);
      }

/* 366 */       objectEnd("network-policies");
    }

/* 369 */     if (!routerConfig.getServerGroups().isEmpty()) {
/* 370 */       objectBegin("server-groups");

/* 372 */       for (Map.Entry<String, ServerGroup> e : routerConfig.getServerGroups().entrySet()) {
/* 373 */         String key = String.valueOf(e.getKey());

/* 375 */         objectBegin(key);
/* 376 */         ((ServerGroup)e.getValue()).accept(this.m_visitor);
/* 377 */         objectEnd(key);
      }

/* 380 */       objectEnd("server-groups");
    }

/* 383 */     if (!routerConfig.getDomains().isEmpty()) {
/* 384 */       objectBegin("domains");

/* 386 */       for (Map.Entry<String, Domain> e : routerConfig.getDomains().entrySet()) {
/* 387 */         String key = String.valueOf(e.getKey());

/* 389 */         objectBegin(key);
/* 390 */         ((Domain)e.getValue()).accept(this.m_visitor);
/* 391 */         objectEnd(key);
      }

/* 394 */       objectEnd("domains");
    }
  }



/* 400 */   public void visitServer(Server server) { attributes(null, new Object[] { "id", server.getId(), "port", Integer.valueOf(server.getPort()), "weight", Double.valueOf(server.getWeight()) }); }



  public void visitServerGroup(ServerGroup serverGroup) {
/* 405 */     attributes(null, new Object[] { "id", serverGroup.getId(), "title", serverGroup.getTitle() });

/* 407 */     if (!serverGroup.getGroupServers().isEmpty()) {
/* 408 */       objectBegin("group-servers");

/* 410 */       for (Map.Entry<String, GroupServer> e : serverGroup.getGroupServers().entrySet()) {
/* 411 */         String key = String.valueOf(e.getKey());

/* 413 */         objectBegin(key);
/* 414 */         ((GroupServer)e.getValue()).accept(this.m_visitor);
/* 415 */         objectEnd(key);
      }

/* 418 */       objectEnd("group-servers");
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultJsonBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */